package com.dcy.znoa.controller.conference;

import static com.dcy.znoa.common.util.pojo.JsonCommon.SUCCESS_CODE;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.excel.ExcelOutput;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.conference.model.conferenceRoom.AddConferenceRoomModel;
import com.dcy.znoa.controller.conference.model.conferenceRoom.SelectConferenceRoomModel;
import com.dcy.znoa.controller.conference.model.conferenceRoom.UpdateConferenceRoomModel;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.conference.ConferenceRoom;
import com.dcy.znoa.service.conference.ConferenceRoomService;

/**
 * @author chenjie
 */
@Controller
@RequestMapping("conferenceRoom")
public class ConferenceRoomController extends BaseController {
    @Autowired
    ConferenceRoomService conferenceRoomService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(SelectConferenceRoomModel selectConferenceRoomModel) throws BusinessException, InvocationTargetException, IllegalAccessException {       
    	//会议室列表：查询本公司会议室  h5
    	Oper oper = getCurrentUser();
    	selectConferenceRoomModel.setCompId(oper.getCompId());
        return conferenceRoomService.selectList(selectConferenceRoomModel);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectStatusList")
    public JsonForList selectStatusList(SelectConferenceRoomModel selectConferenceRoomModel) throws BusinessException, InvocationTargetException, IllegalAccessException {       
    	//会议室列表：该公司所有会议室列表及其当前状态（空闲和使用中） app
    	Oper oper = getCurrentUser();
    	selectConferenceRoomModel.setCompId(oper.getCompId());
        return conferenceRoomService.selectConferenceRoomStatus(selectConferenceRoomModel);
    }
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddConferenceRoomModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonCommon("404",fieldError.getDefaultMessage());
        }
        if(model.getConferName().length()>50){
            return  new JsonCommon("404","会议室名称长度不能大于50");
        }
        
        Oper oper = getCurrentUser();
    	model.setCreateOperName(oper.getOperName());
        model.setCompId(getCurrentUser().getCompId());
        
        ConferenceRoom conferenceRoom = new ConferenceRoom();
        BeanUtils.copyProperties(conferenceRoom, model);
        return conferenceRoomService.insert(conferenceRoom);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    //修改、禁用、启用
    public JsonCommon updateById(@Valid UpdateConferenceRoomModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonCommon("404",fieldError.getDefaultMessage());
        }
        if(model.getConferName()!=null && model.getConferName().length()>50){
            return  new JsonCommon("404","会议室名称长度不能大于50");
        }
        
        model.setUpdateOperName(getCurrentUser().getOperName());
        ConferenceRoom conferenceRoom = new ConferenceRoom();
        BeanUtils.copyProperties(conferenceRoom, model);
        return conferenceRoomService.updateById(conferenceRoom);
    }
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/export")
    public JsonCommon export(@Valid SelectConferenceRoomModel selectConferenceRoomModel,BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) throws BusinessException, InvocationTargetException, IllegalAccessException, IOException {
        if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonCommon("404",fieldError.getDefaultMessage());
        }

        //先查询会议室列表：查询本公司会议室  h5
    	Oper oper = getCurrentUser();
    	selectConferenceRoomModel.setCompId(oper.getCompId());
    	JsonForList jsonForList = conferenceRoomService.selectList(selectConferenceRoomModel);      
        
        List<Map<String, Object>> recordList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();//list转map
        List<ConferenceRoom> list = jsonForList.getData();
        for (ConferenceRoom conferenceRoom :list) {
            recordList.add(ConferenceRoom.toMap(conferenceRoom));
        }
        
        String keys[] = {"conferId", "conferType", "conferName", "status", "conferCapacity", "conferAddress"};//map中的key
        String columnNames[] = {"ID", "会议室类型", "会议室名称", "会议室状态", "容纳人数", "地址"};//列名
        ExcelOutput.ExcelOutput(request, response, recordList, keys, columnNames, "会议室明细");
        
        return new JsonCommon(SUCCESS_CODE);
    }

}
